# -*- coding: utf-8 -*-
"""
Created on Wed Feb 07 10:18:20 2018

@author: beall
"""

import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
import test_runner as test

def benchmark2():
    m = GEKKO()

    nt = 101
    m.time = np.linspace(0,1,nt)

    # Parameters
    u = m.MV(value=9,lb=-4,ub=10)
    u.LOWER = -4
    u.UPPER = 10
    u.STATUS = 1
    u.DCOST = 0

    # Variables
    t = m.Var(value=0)
    x1 = m.Var(value=0)
    x2 = m.Var(value=-1)
    x3 = m.Var(value=-np.sqrt(5))
    x4 = m.Var(value=0)

    p = np.zeros(nt)
    p[-1] = 1.0
    final = m.Param(value=p)

    # Equations
    m.Equation(t.dt()==1)
    m.Equation(x1.dt()==x2)
    m.Equation(x2.dt()==-x3*u+16*t-8)
    m.Equation(x3.dt()==u)
    m.Equation(x4.dt()==x1**2+x2**2 \
            +0.0005*(x2+16*t-8-0.1*x3*(u**2))**2)

    # Objective Function
    m.Obj(x4*final)

    m.options.IMODE = 6
    m.options.NODES = 4
    m.options.MV_TYPE = 1
    m.options.SOLVER = 3
    m.solve(disp=False)
    assert test.like(x1.value, [0.0, -0.009368626, -0.01741699, -0.02419229, -0.02977851, -0.03425967, -0.03771975, -0.04024277, -0.04192643, -0.04291645, -0.0433505, -0.04334679, -0.04300506, -0.04240779, -0.04162158, -0.04069902, -0.03968099, -0.03859966, -0.03748179, -0.03635218, -0.03523704, -0.03416827, -0.03319585, -0.0323983, -0.03186183, -0.03167063, -0.0318685, -0.0324511, -0.03340146, -0.03470331, -0.03634098, -0.03829863, -0.04056028, -0.04310992, -0.04593157, -0.04900922, -0.05232686, -0.05586851, -0.05961816, -0.0635598, -0.06767745, -0.0719551, -0.07637674, -0.08092639, -0.08558803, -0.09034568, -0.09518333, -0.100085, -0.1050346, -0.1100163, -0.1150139, -0.1200116, -0.1249932, -0.1299429, -0.1348445, -0.1396821, -0.1444398, -0.1491014, -0.1536511, -0.1580727, -0.1623504, -0.166468, -0.1704097, -0.1741593, -0.177701, -0.1810186, -0.1840963, -0.1869179, -0.1894676, -0.1917292, -0.1936868, -0.1953245, -0.1966262, -0.1975762, -0.1981612, -0.1983828, -0.1983141, -0.1980392, -0.1976397, -0.1971786, -0.196689, -0.1961938, -0.1957082, -0.1952421, -0.1948013, -0.1943887, -0.1940053, -0.1936511, -0.1933253,-0.193027, -0.1927559, -0.1925117, -0.1922955, -0.1921102, -0.1919615, -0.1918601, -0.1918249, -0.191886, -0.1920885, -0.1924952, -0.1931884])
    assert test.like(x2.value, [-1.0, -0.8712899, -0.739783, -0.616676, -0.5019691, -0.3956622, -0.2977552, -0.2082483, -0.1312033, -0.06908596, -0.01970684, 0.01878202, 0.04818417, 0.07015813, 0.08620795, 0.09762844, 0.1054484, 0.11038, 0.1127917, 0.1127033, 0.1098264, 0.1031859, 0.08989843, 0.06821099, 0.03759608, -0.0001403682, -0.03928998, -0.07693181, -0.1128725, -0.1472381, -0.1800315, -0.2112314, -0.2408314, -0.2688314, -0.2952313, -0.3200313, -0.3432313, -0.3648313, -0.3848313, -0.4032313, -0.4200313, -0.4352313, -0.4488313, -0.4608313, -0.4712313, -0.4800313, -0.4872313, -0.4928313, -0.4968313, -0.4992313, -0.5000313, -0.4992313, -0.4968313, -0.4928313, -0.4872313, -0.4800313, -0.4712313, -0.4608313, -0.4488313,-0.4352313, -0.4200313, -0.4032313, -0.3848314, -0.3648314, -0.3432314, -0.3200314, -0.2952314, -0.2688314, -0.2408314, -0.2112314, -0.1800314, -0.1472317, -0.1128321, -0.07698039, -0.03946273, -0.006223653, 0.01858121, 0.0349861, 0.04382554, 0.04793561, 0.04955784, 0.04924218, 0.04770908, 0.04541116, 0.04269758, 0.03980118, 0.03687116, 0.03398973, 0.03118934, 0.0284641, 0.02577484, 0.02304462, 0.02014036, 0.01683438, 0.01274009, 0.007224165, -0.0006756298, -0.01228945, -0.02925142, -0.05347612, -0.08681907])
    assert test.like(x3.value, [-2.236068, -2.141069, -2.041069, -1.941069, -1.841069, -1.741069, -1.641069, -1.541069, -1.443885, -1.35195, -1.264678, -1.181538, -1.101829, -1.024734, -0.9493052, -0.8744938, -0.7991867, -0.7222439, -0.642505, -0.558739, -0.4694065, -0.3731255, -0.2731257, -0.1731257, -0.07999328, -0.01547976, 0.01099767, 0.01418825, 0.009093174, 0.003711985, 0.0005919895, -0.0004832268, -0.0005056367, -0.0002498527, -5.763804e-05, 1.738263e-05, 2.431157e-05, 1.194955e-05, 2.373141e-06, -1.022264e-06, -1.093399e-06, -4.338649e-07, -3.316025e-08, 6.465095e-08, 4.110701e-08, 9.704644e-09, -2.45684e-09, -2.94259e-09, -9.979606e-10, 3.20562e-11, 1.815426e-10, 7.526608e-11, 3.042189e-12, -1.156162e-11, -7.2881e-12, 5.626169e-15, 2.020845e-11, 4.928653e-11, -2.835956e-11, -4.660837e-10, -8.961849e-10, 1.909341e-09, 1.241169e-08, 9.505695e-09, -1.010082e-07, -2.981075e-07, 5.308718e-07, 4.285289e-06, 8.78398e-08, -5.522232e-05, -5.722639e-05, 0.0008038919, 0.001106048, -0.01670707, 0.01296086, 0.1099515, 0.2099514, 0.3099513, 0.40791, 0.4993685, 0.5835442, 0.6623856, 0.7366278, 0.8069796, 0.8739811, 0.9381022, 0.9997478, 1.059273, 1.116987, 1.173162, 1.228042, 1.281853, 1.334825, 1.387216, 1.43936, 1.491707, 1.544859, 1.599568, 1.656665, 1.717029, 1.781369])
    assert test.like(x4.value, [0.0, 0.009391499, 0.01664753, 0.02193173, 0.02566607, 0.02821239, 0.02987672, 0.03091364, 0.03147557, 0.03172517, 0.03182485, 0.03186824, 0.03190596, 0.03196116, 0.03204101, 0.03214438, 0.03226649, 0.03240157, 0.03254382, 0.03268761, 0.03282702, 0.03295587, 0.03306983, 0.03316673, 0.03325437, 0.03334324, 0.03343601, 0.03355436, 0.03372486, 0.03397063, 0.03431162, 0.03476456, 0.03534308, 0.03605795, 0.03691721, 0.03792629, 0.03908824, 0.04040382, 0.04187168, 0.04348851, 0.04524918, 0.04714691, 0.04917341, 0.05131902, 0.05357288, 0.05592305, 0.05835669, 0.06086019, 0.06341931, 0.06601937, 0.06864532, 0.07128197, 0.0739141,0.07652658, 0.07910457, 0.08163364, 0.0840999, 0.08649021, 0.08879225, 0.0909947, 0.09308744, 0.0950616, 0.09690979, 0.09862622, 0.1002068,0.1016495, 0.1029542, 0.104123, 0.1051604, 0.1060734, 0.1068717, 0.1075678, 0.1081772, 0.1087187, 0.1092139, 0.1096681, 0.1100938, 0.1105085, 0.110921, 0.1113354, 0.1117515, 0.1121676, 0.1125827, 0.1129968, 0.1134098, 0.1138224, 0.114235, 0.1146481, 0.1150621, 0.1154773, 0.1158941, 0.1163123, 0.1167319, 0.1171523, 0.1175723, 0.1179901, 0.1184032, 0.1188089, 0.1192077, 0.1196071, 0.1200303])

test.test('benchmark2', benchmark2)
